//
//  TextView.swift
//  ASW
//
//  Created by zhuxuhong on 2020/11/13.
//  Copyright © 2020 agoculture. All rights reserved.
//

import UIKit
import SnapKit

open class TextView: TextInputView {
    private lazy var placeholderLabel: Label = {
        let v = Label()
        v.font = UIFont.systemFont(ofSize: 17)
        v.textColor = placeholderColor ?? UIColor.black.withAlphaComponent(0.4)
        v.text = placeholderText
        v.numberOfLines = 0
        return v
    }()
    public lazy var input: UITextView = {
        let v = UITextView(frame: .zero)
        v.textAlignment = .left
        v.autocapitalizationType = .none
        v.autocorrectionType = .no
        v.spellCheckingType = .no
        v.enablesReturnKeyAutomatically = true
        v.keyboardType = .default
        v.font = UIFont.systemFont(ofSize: 17)
        v.textColor = .black
        v.delegate = self
        v.backgroundColor = .clear
        v.textContainerInset = .zero
        v.tintColor = tintColor
        return v
    }()
    
    public override var text: String?{
        didSet{
            input.text = text
        }
    }
    public override var placeholderText: String? {
        didSet{
            placeholderLabel.text = placeholderText
        }
    }
    open override var placeholderColor: UIColor?{
        didSet{
            placeholderLabel.textColor = placeholderColor
        }
    }
    
    open override var tintColor: UIColor! {
        didSet{
            input.tintColor = tintColor
        }
    }
    
    open override func didInit() {
        super.didInit()
        setup()
    }
    open override func didAwake() {
        super.didAwake()
        setup()
    }
    private func setup() {
        addSubview(placeholderLabel)
        addSubview(input)
        input.snp.makeConstraints { make in
            make.top.bottom.trailing.equalToSuperview()
            make.leading.equalTo(-4)
        }
        placeholderLabel.snp.makeConstraints { make in
            make.leading.top.trailing.equalToSuperview()
        }
        input.text = text
    }
}

extension TextView: UITextViewDelegate {
    public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        input.resignFirstResponder()
    }
    public func textViewDidChange(_ textView: UITextView) {
        let text = textView.text ?? ""
        placeholderLabel.isHidden = !text.isEmpty
        inputChanged?(.init(status: .changed, text: text, textView: self))
    }
    public func textViewDidBeginEditing(_ textView: UITextView) {
        inputChanged?(.init(status: .began, text: textView.text, textView: self))
    }
    public func textViewDidEndEditing(_ textView: UITextView) {
        inputChanged?(.init(status: .ended, text: textView.text, textView: self))
    }
    public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "" {
            return true
        }
        if text == "\n" {
            guard let keyed = inputReturnKeyed else {
                return true
            }
            resignFirstResponder()
            return keyed(.init(text: text, textView: self))
        }
        if maxWords > 0, let text = textView.text, text.count >= maxWords {
            return false
        }
        return inputShouldContinue?(.init(range: range, string: text, text: textView.text, textView: self)) ?? true
    }
}
